<?php
/**
 * Copyright (c) 2011 Georg Ehrke <ownclouddev at georgswebsite dot de>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */
if(!OCP\User::isLoggedIn()) {
   OCP\User::checkLoggedIn();
}
OCP\JSON::checkAppEnabled('calendar');

$jobid = JRequest::getInt('jobid', 0);

if(!PCompanyHelper::checkJobManagerPermissions($jobid)) {
   echo '对不起，您没有权限访问该资源！';
   exit();
}

$start = JFactory::getDate()->toUnix();
$start = $start - ($start%(30*60)) + 30*60;

$calendar_model = new PCalendarModelCalendar();
$calendars = $calendar_model->allCalendars(OC_USER::getUser());

$calendar_options = array();

foreach($calendars as $calendar) {
   if($calendar['userid'] != OCP\User::getUser()) {
      $sharedCalendar = OCP\Share::getItemSharedWithBySource('calendar', $calendar['id']);
      if ($sharedCalendar && ($sharedCalendar['permissions'] & OCP\PERMISSION_UPDATE)) {
         array_push($calendar_options, $calendar);
      }
   } else {
      array_push($calendar_options, $calendar);
   }
}
// Get notify tpl list
$tpl_model = new PCalendarModelTpl();
$tpl_list = $tpl_model->getAllTpl('notify');
$user = PFactory::getMasterUser();
$companyinfo = JobHelper::getEmployerProfileDetails($user->id);

$calendar = new PCalendarModellistview();
$interview_list = $calendar->getAttachmentList('interview');

$ex_data = new stdclass();
$ex_data->jobseekerid = JRequest::getInt('jobseekerid', 0);
$ex_data->type = JRequest::getInt('type', 0);
$ex_data->source = JRequest::getString('source', '');

$ex_data->jobseekername = JRequest::getString('jobseekername', '');
$ex_data->round = JRequest::getInt('round', 0);
$ex_data->jobid = $jobid;
$ex_data->job_title = JRequest::getString('jobtitle', '');
$ex_data->validemail = JRequest::getInt("validemail", 0);
$ex_data->objectid = JRequest::getInt('objectid', 0);
//tpl fields
$ex_data->notify_text = "";
$ex_data->address = "";
$ex_data->contact_phone = "";
$ex_data->contact = "";
if(empty($ex_data->objectid)) {
   if(!empty($tpl_list)){
      $ex_data->notify_text = !empty($tpl_list[count($tpl_list)-1]['tpl_text']) ? $tpl_list[count($tpl_list)-1]['tpl_text'] : '';
      $ex_data->address = !empty($tpl_list[count($tpl_list)-1]['address']) ? $tpl_list[count($tpl_list)-1]['address'] : '';
      $ex_data->contact_phone = !empty($tpl_list[count($tpl_list)-1]['contact_phone']) ? $tpl_list[count($tpl_list)-1]['contact_phone'] : '';
      $ex_data->contact = !empty($tpl_list[count($tpl_list)-1]['contact']) ? $tpl_list[count($tpl_list)-1]['contact'] : '';
   }
   if(empty($ex_data->address)) {
      $ex_data->address =  $companyinfo->address;
   }
   if(empty($ex_data->contact_phone)) {
      $ex_data->contact_phone = $companyinfo->phone;
   }
} else {
   $object_model = new PCalendarModelObject();
   $extra_data = $object_model->getEventExtraData($ex_data->objectid);
   if(!empty($extra_data)) {
      $ex_data->notify_text = !empty($extra_data->notify_text) ? $extra_data->notify_text : '';
      $ex_data->address = !empty($extra_data->address) ? $extra_data->address : '';
      $ex_data->contact_phone = !empty($extra_data->contact_phone) ? $extra_data->contact_phone : '';
      $ex_data->contact = !empty($extra_data->contact) ? $extra_data->contact : '';
      $ex_data->summary = !empty($extra_data->summary) ? $extra_data->summary : '';
      $ex_data->is_phone = !empty($extra_data->is_phone) ? $extra_data->is_phone : '';
      $ex_data->attachment = !empty($extra_data->attachment) ? $extra_data->attachment : '';
      $ex_data->file_name = !empty($extra_data->file_name) ? $extra_data->file_name : '';
      $ex_data->totalfeedbacks = !empty($extra_data->totalfeedbacks) ? $extra_data->totalfeedbacks : '';
      $ex_data->last_round_totalfeedbacks = !empty($extra_data->last_round_totalfeedbacks) ? $extra_data->last_round_totalfeedbacks : '';
   }
}

$tmpl = new OCP\Template('calendar', 'part.newevent');
$tmpl->assign('access', 'owner');
$tmpl->assign('accessclass', 'PUBLIC');
$tmpl->assign('calendar_options', $calendar_options);

$tmpl->assign('eventid', 'new');

$tmpl->assign('startdate', JHtml::_("date", $start, 'Y-m-d'));
$tmpl->assign('starttime', JHtml::_("date", $start, 'H:i'));
$tmpl->assign('ex_data', $ex_data);

$tmpl->assign('tpl_list', $tpl_list);
$tmpl->assign('interview_list', $interview_list);
if(isset($ex_data->last_round_totalfeedbacks)) {
   $tmpl->assign('totalfeedbacks', $ex_data->last_round_totalfeedbacks);
}

$tmpl->printpage();
